var http = require("../../utils/http.js")
// pages/maintenanceRecordInfo/maintenanceRecordInfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'',
    dataList:{},
    role:'',
    state:'',
    image:null,
    states:'待维修'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type,
      role:JSON.parse(wx.getStorageSync('Security')).role
    })
    http.get('api/Repairs/GetRepairs?id='+options.type,(data)=>{
      // http.get('api/Repairs/GetRepairs?id=1',(data)=>{
      if(data.code==200){
        this.setData({
          dataList:data.repairs
        })
        if(data.repairs.state==0)
        {
          this.setData({
            states:'待维修'
          })
        }else if(data.repairs.state==1)
        {
          this.setData({
            states:'已维修'
          })
        }else{
          this.setData({
            states:'结束'
          })
        }
        // console.log(this.data.dataList)
        console.log(data.repairs.picture)

        // wx.request({
        //   url: 'http://localhost:5000/api/Image/ShowImage?str='+data.repairs.picture,
        //   success:(datas)=>{
        //     console.log(datas)

        //     this.setData({
        //       image:'data:image/png;base64,'+wx.arrayBufferToBase64(datas.data)
        //     })
        //     // console.log(this.data.image)
        //   }

        // })

        http.get('api/Image/ShowImage?str='+data.repairs.picture,(datas)=>{
          // console.log(datas)
          this.setData({
              image:'http://localhost:5000/api/Image/ShowImage?str='+encodeURI(data.repairs.picture)
            })
            // console.log(this.data.image)
        })

      }
    })
  },
  onSubmit(e){
    console.log(e.detail.value)
    console.log(this.data)

    http.post('api/Repairs/EditRepairs',{ID:parseInt(this.data.type),...e.detail.value,state:parseInt(e.detail.value.state)},(data)=>{
      if(data.code==200)
      {
        wx.navigateTo({
          url: '/pages/maintenanceRecord/maintenanceRecord?type=1',
        })
        wx.showToast({
          title: data.message,
          icon:'success',
          duration:1000
        })

      }else{
        wx.showToast({
          title: data.message,
          icon:'error',
          duration:1000
        })
      }
      console.log(data)
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})